from typing import List
from PyQt5.QtGui import QMouseEvent, QPen, QPainter, QPaintEvent
from PyQt5.QtWidgets import QWidget, QGraphicsItem
from PyQt5.QtCore import Qt, QPoint

from Tools.Transform.Controller import TransformEvent
from GUI.Widget.ControllerTag import ControllerTag


class ControllerTag(ControllerTag):

    def __init__(self, parent, controller=None):
        """
        :param parent:  QWidget
        :return QWidget #handle of the controller
        """
        super().__init__(parent)
        self.initUI()

    def initUI(self):
        self.resize(20, 20)
        self.show()

    def paintEvent(self, event: QPaintEvent):
        # 绘制圆形
        qp = QPainter()
        pen = QPen(Qt.blue, 5, Qt.SolidLine)
        qp.begin(self)
        qp.setPen(pen)
        qp.drawEllipse(self.width()/2, self.height()/2, 5, 5)
        qp.end()

    def mousePressEvent(self, event: QMouseEvent):
        if event.button() == Qt.LeftButton:
            print('tag: 按下鼠标左键, index:', self.Index)
            controller = self.controller
            controller.Active = True
            controller.OperatingTag = self
            self.Parent.PreviousMousePos = self.mapToParent(event.pos())
            event.accept()

    def mouseReleaseEvent(self, event: QMouseEvent):
        if event.button() == Qt.LeftButton:
            print('tag: 释放鼠标左键')
            controller = self.controller
            controller.Active = False
            controller.OperatingTag = None
            event.accept()

    def move2(self, x, y):
        """
        移动部件 中心到（xRate,yRate)
        :param x: int
        :param y: int
        :return:
        """
        self.move(x - self.width()/2, y - self.height()/2)

    @property
    def pos2(self):
        """
        中心坐标
        :return:
        """
        return QPoint(self.x() + self.width()/2, self.y() + self.height()/2)

    @property
    def x2(self):
        """
        中心坐标
        :return:
        """
        return self.x() + self.width()/2

    @property
    def y2(self):
        """
        中心坐标
        :return:
        """
        return self.y() + self.height() / 2


class Controller:
    Name = 'Controller'
    TagList: List[ControllerTag] = []  # 部件列表,操作单元， 响应鼠标按下和释放事件
    Active = False  # 正在编辑
    graphicsItem: QGraphicsItem = None  # 指示器目标QGraphicsItem
    layer = None  # 所属图层

    OperatingTag: QWidget = None  # 正在操作的Tag
    # Operator: GeometryOperator = None  # 当前操作指示器的动作
    editorWidget = None
    Parent = None  # 父面板对象：ControlledPanel

    def __init__(self, parent, layer) -> QWidget:
        """
        :param layer:
        :param parent:  QWidget
        :return QWidget #handle of the indicator
        """
        self.Parent = parent
        self.layer = layer
        self.graphicsItem = layer.graphicsItem
        layer.isEditing = True
        layer.controller = self

    def setEditorWidget(self, editorWidget):
        # 编辑组件
        pass

    def onTransformEvent(self, event: TransformEvent):
        """
        应该被子类 重写
        :param event: 变换事件
        """
        return

    def destroy(self):
        # 图层保存
        layer = self.layer
        layer.isEditing = False
        layer.controller = None

    def confirm(self):
        self.destroy()

    def cancel(self):
        self.destroy()

    def aboutSave(self):
        self.Parent.aboutSave()